<?php


namespace App;


use Model\Model;

class RegisterController extends BaseController
{
    function post($request){
        $name = $this->valid('name',$request);
        $password = $this->valid('password',$request);
        $nickname = $this->valid('nickname',$request);
        $desc = $this->valid('desc',$request);


        if(!($name && $password && $nickname && $desc))
            $this->response('参数错误');
        //执行mysql逻辑
        $model = new Model();
        //判断名字是否重复
        $st = $model->prepare("select * from `tbUsers` where `username`=?");
        $st->bindParam(1, $name);
        $st->execute();
        $res = $st->fetchAll();
        if($res != [])
            $this->response('用户名存在');
        //注册逻辑
        $st = $model->prepare("insert into `tbUsers` (`username`,`password`,`nickname`,`desc`) values (?,?,?,?)");
        $st->bindParam(1, $name);
        $st->bindParam(2, $password);
        $st->bindParam(3, $nickname);
        $st->bindParam(4, $desc);
        $res = $st->execute();
        //关闭链接
        $model =null;
        if($res)
            $this->response('注册成功 请登录');
        else
            $this->response('注册失败');

    }

    function get($request){
        $html = $this->display('register.html');
        $this->response($html);
    }

}